using CfgTable;
using IQIGame.Onigao.Framework;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 简单的状态切换器
    /// </summary>
    public class LevelSimpleStateComponent : LevelEntityBaseComponent
    {
        public EntityParamSwitchState switchState { get; private set; }

        public CfgLevelEntityState cfgEntityState { get; private set; }

        public int currentStateIndex { get; private set; }

        public float currentTimer { get; private set; }

        public float curStateTotalTime { get; private set; }

        /// <summary>
        /// 初始化,带简易状态机的实体
        /// </summary>
        /// <param name="entityParamElevator"></param>
        public void InitSimpleStateEntity(EntityParamSwitchState switchState)
        {
            this.switchState = switchState;
            cfgEntityState = TableCenter.levelEntityState.Get(switchState.stateCid);
            if (cfgEntityState == null)
            {
                LogGame.LogError("[LevelSimpleStateComponent] entityState cfg is null,id:" + switchState.stateCid);
                return;
            }


            if (cfgEntityState.InitialState >= cfgEntityState.AnimAction.Count)
            {
                LogGame.LogError("[LevelSimpleStateComponent] init state over flow,id:" + switchState.stateCid);
            }

            currentStateIndex = cfgEntityState.InitialState;
            currentTimer = cfgEntityState.ElapsedTimeOnInit.MillisecToSec();
            this.SwitchState(cfgEntityState.InitialState);
        }

        /// <summary>
        /// 切换状态
        /// </summary>
        /// <param name="index"></param>
        private void SwitchState(int index)
        {

            curStateTotalTime = cfgEntityState.ActionDuration[currentStateIndex].MillisecToSec();
            var normalizedTime = currentTimer / curStateTotalTime;
            PlayCurrentState(normalizedTime);

            MsgDispatcher.Broadcast<LevelPlayBaseEntity, EntityParamSwitchState, int>(MsgEventType.Level_EntityStateChanged, this.entity, this.switchState, this.currentStateIndex);
        }

        /// <summary>
        /// 播放当前状态的动画
        /// </summary>
        /// <param name="normalizedTime"></param>
        private void PlayCurrentState(float normalizedTime)
        {
            var anim = this.entity.GetAnimationComponent();
            if (anim == null)
            {
                return;
            }
            anim.StartAnimAction(this.cfgEntityState.AnimAction[this.currentStateIndex], normalizedTime);
        }

        public override void OnUpdate(float deltaTime)
        {
            this.currentTimer += deltaTime;

            if (this.currentTimer >= curStateTotalTime)
            {
                this.currentStateIndex++;
                if (currentStateIndex >= cfgEntityState.AnimAction.Count)
                {
                    this.currentStateIndex = 0;
                }
                currentTimer = 0;
                SwitchState(this.currentStateIndex);
            }
        }

        #region ObjectPool

        public override uint MaxStore => 10;

        public override void OnPoolReset()
        {
            entity = null;
            switchState = null;
            cfgEntityState = null;
            currentStateIndex = 0;
            currentTimer = 0;
            curStateTotalTime = 0;
        }

        #endregion
    }
}